<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class UserController extends Controller {
	
	private $pageSize = 10;
	
    public function index(){
		$p = I('get.p');
		if(empty($p)){
			$p = 1;
		}
		$list = M('User')->limit(($p-1)*$this->pageSize.','.$this->pageSize)->select(); 
		$this->assign('list',$list);
		$count      = M('User')->count();
		$Page       = new Page($count,$this->pageSize);
		$show = $Page->showhtml();
		$this->assign('page',$show);
		$this->display();  
    }
	
	public function export(){
		$ct = 50;
		$p = 1; 
		set_time_limit(0); 
		
		header( "Cache-Control: public" );
		header( "Pragma: public" );
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=user.csv");
		header('Content-Type:APPLICATION/OCTET-STREAM'); 
		ob_start();
		
		$header_str =  "Channel,获取渠道,Media,经销商代码,经销商名称,客户姓名,性别,手机,电子邮箱,意向车型,购车时间,省份,城市,地址,邮编,活动城市,邀请参与活动日期,邀请参与活动场次,是否签到(Y/N),签到时间,捷豹知识挑战(得分),反应极限挑战(得分),我的英仕锋尚(得分),驭享非凡座驾(得分),私人订制大片(得分),总得分,活动满意度,品牌推荐度\n";  
		
		$header_str = mb_convert_encoding($header_str, "GBK", "UTF-8");
		
		$file_str = '' ;
		$list = M('User')->limit(($p-1)*$ct.','.$ct)->select();
		while($list){ 
			foreach($list as $li){
				$sc = M('Score')->where('uid='.$li['id'])->find();
				if(empty($sc)){
					$sc = array(
						'g1_value'=>'',
						'g1_score'=>0,
						'g1_created'=>'',
						'g1_status'=>'',
						'g2_value'=>'',
						'g2_score'=>0,
						'g2_created'=>'',
						'g3_status'=>'',
						'g3_value'=>'',
						'g3_score'=>0,
						'g3_created'=>'',
						'g3_status'=>'',
						'g4_value'=>'',
						'g4_score'=>0,
						'g4_created'=>'',
						'g4_status'=>'',
						'g5_value'=>'',
						'g5_score'=>0,
						'g5_created'=>'',
						'g5_status'=>'',
					);
				}
				$tsc = $sc['g1_score'] + $sc['g2_score'] + $sc['g3_score'] + $sc['g4_score'] + $sc['g5_score'] ;
				$fArr = array(
					$li['channel'] 			//Channel,
					,$li['comefrom']		//获取渠道,
					,$li['media'] 			//Media,
					,$li['jxs_code']		//经销商代码,
					,$li['jxs_name']		//经销商名称,
					,$li['username']		//客户姓名,
					,$li['sex']				//性别,
					,$li['phone']			//手机,
					,$li['email']			//电子邮箱,
					,$li['cartype']			//意向车型,
					,$li['buy_time']		//购车时间,
					,$li['sheng']			//省份,
					,$li['shi']				//城市,
					,$li['addr']			//地址,
					,$li['qu']				//邮编,
					,$li['city']			//活动城市,
					,$li['act_time']		//邀请参与活动日期,
					,$li['act_index']		//邀请参与活动场次,
					,($li['sign']==1)?'Y':'N'	//是否签到(Y/N),
					,$li['signtime']		//签到时间,
					,$sc['g4_score']		//捷豹知识挑战(得分),
					,$sc['g3_score']		//反应极限挑战(得分),
					,$sc['g5_score']		//我的英仕锋尚(得分),
					,$sc['g2_score']		//驭享非凡座驾(得分),
					,$sc['g1_score']		//私人订制大片(得分),
					,$tsc					//总得分,
					,$li['manyi']			//活动满意度,
					,$li['tuijian']			//品牌推荐度
				); 
				$file_str .=  implode(',',$fArr)."\n";
			}
			$p++;
			$list = M('User')->limit(($p-1)*$ct.','.$ct)->select();
		}  
		$file_str= mb_convert_encoding($file_str, "GBK", "UTF-8");  
		ob_end_clean();
		echo $header_str;
		echo $file_str;
	} 
  
  
  	function template(){
		header( "Cache-Control: public" );
		header( "Pragma: public" );
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=template.csv");
		header('Content-Type:APPLICATION/OCTET-STREAM'); 
		ob_start();
		$data_str =  "Channel,Media,经销商代码,经销商名称,媒体姓名,性别,手机,电子邮箱,意向车型,购车时间,省份,城市,地址,邮编,活动城市,邀请参与活动日期,邀请参与活动场次\n";  
		$data_str .= 'KOL,羊城晚报佛山站,　,,邱凯东,先生,13929909341,,JAXF,4-6个月,,,,,佛山,2016/8/9,13:00-15:30';
		ob_end_clean();
		echo $data_str; 
	}
	
	
  	function import(){
		
		$file = $_FILES['file'];
		if(empty($file)){ 
			$this->error('请上传文件');
		}
		$pathinfo = pathinfo($file['name']);
		if(strtolower($pathinfo['extension'])!='csv'){
			$this->error('文件格式错误'); 
		}
		$filename = date('Ymd_His').'_'.mt_rand(10000,99999).'.csv';
		$arr = array();
		
		if(!move_uploaded_file($file['tmp_name'],'./Public/tmp/'.$filename)){
			$this->error('文件保存失败'); 
		}
		
		$key = array(
			'channel',
			'media',
			'jxs_code',
			'jxs_name',
			'username',
			'sex',
			'phone',
			'email',
			'cartype', 
			'buy_time',
			'sheng',
			'shi', 
			'addr',
			'qu', 
			'city',
			'act_time',
			'act_index'
		);
		
		$f = fopen('./Public/tmp/'.$filename,'r');
		$row = fgetcsv($f);
		
		$result = array();
		
		while($row = fgetcsv($f)){
			$dat = array();
			foreach($key as $k => $v){
				$dat[$v] = $row[$k];
			}
			$tar = array(
				'phone'		=>	$dat['phone'],
				'username'	=>	$dat['username'],
			);
			$us = M('User')->where(array('phone'=>$dat['phone']))->find(); 
			if(count($us)>0){
				$tar['_ST'] = '<span class="label label-warning">Phone Has In</span>';
			}else{
				$rs = M('User')->data($dat)->add();
				if($rs){
					$tar['_ST'] = '<span class="label label-success">Import OK</span>';
				}else{
					$dat['_ST'] = '<span class="label label-danger">Import Fail</span>';
				}
			}
			$result[] = $tar;
		}
		
		$this->dlist = $result;
		$this->display();
	}
}